	TITLE	DISK SUBSYSTEM FOR SYSTEM MONITOR
;
;****Donated to the Pascal/Z80 Users Group, July 1980*******
;
;	FLOPPY DISK SUBSYSTEM FOR THE SYSTEM MONITOR
;	IT INTERTIES CP/M TO THE MONITOR SO THAT BOTH
;	PACKAGES OPERATE AS AN INTEGRATED WHOLE
;
;	MODIFIED FOR C-MACRO ASSEMBLER MAY 1979
;
;	DEFINE SYMBOLS
;
;SYSTEM BASES
;MBASE IS MONITOR BASE ADDR
;CBASE IS CONSOLE SUBSYSTEM BASE ADDR
;DWBASE IS THE DISK VARIABLE RAM BASE ADDR
;FBBASE IS CP/M BIOS BASE ADDR
;
;EXTERNAL CALLS
;
DCNSTA	DEFL	CBASE+3
DCONIN	DEFL	CBASE+6
DCNOUT	DEFL	CBASE+24
DLIST	DEFL	CBASE+12
DBUFFO	DEFL	CBASE+21H	;OUTPUT A MESSAGE TO CONSOLE
GOBACK	DEFL	MBASE+3		;RESTART MONITOR
LOADER	DEFL	FBBASE+2DH	;SD SALES LOADER
;
;	THIS SUBSYSTEM WILL PATCH THE CP/M
;	BIOS VECTOR JUMP TABLE ON THE END OF BDOS OF CP/M
;	TO TRAP CONSOLE I/O FOR MONITOR INTERTIE.
;
;INTERNAL SYMBOLS
;
CR	DEFL	0DH
LF	DEFL	0AH
EOM	DEFL	00H
FCB	DEFL	DWBASE+10
BDOS	DEFL	05H
INITCM	DEFL	13
OPENCM	DEFL	15
READCM	DEFL	20
SDMACM	DEFL	26
STATCM	DEFL	11
;
;ENTRY VECTORS
;
DBASE	EQU	$
DNTRY0	JP	DINIT	;DISK SUBSYSTEM INITIALIZATION
DNTRY1	JP	DMAIN	;DISK TRANSIENT FILE LOADER
DNTRY2	JP	DTRAP	;DISK BOOT TRAP VECTOR
;
;DEFINED CONSTANTS FOR SUBSYSTEM
;
DWORK	DEFW	DWBASE	;VARIABLE RAM ADDR POINTER
;
;	INITIALIZE THE SUBSYSTEM
;
DINIT	CALL	DSETUP
	LD	A,0FFH
	LD	(IY-1),A
	POP	IY
	RET
;
DSETUP	EX	(SP),IY
	PUSH	IY
	LD	IY,(DWORK)
	RET
;
;	BOOT TRAP VECTOR
;
DTRAP	CALL	DCONIN
	CP	03H
	JP	Z,GOBACK	;CONTROL-C ENCOUNTER
	RET			;ELSE RETURN TO TRANSIENT
;
;	DISK SUBSYSTEM MAIN ENTRY
;
DMAIN	CALL	DSETUP
	PUSH	AF
	PUSH	BC
	PUSH	IX
	LD	HL,0
	ADD	HL,SP
	LD	(IY-3),L
	LD	(IY-2),H
	JP	BOOT
;
;	CONTINUE WITH LOADING REQUESTED UTILITY
;
DBACK	LD	L,(IY-3)
	LD	H,(IY-2)
	LD	SP,HL
	POP	IX
	POP	BC
	PUSH	IY
	POP	DE
	XOR	A
	LD	(DE),A
	INC	DE
	POP	AF
	OR	A
	JP	Z,DCOPY
	JP	DVERFY
;
;	COPY COMMAND FILE SET
;
DCOPY	LD	HL,CPYBLK
	JP	BFCB
;
;	VERIFY COMMAND FILE SET
;
DVERFY	LD	HL,VFYBLK
;
;	BUILD FILE CONTROL BLOCK
;
BFCB	PUSH	BC	;BUILD COPY OR VRFY
	LD	BC,4
	LDIR
	POP	BC	;BUILD SOURCE/DESTINATION CODE
	LD	A,B
	LD	(DE),A
	INC	DE
	LD	A,C
	LD	(DE),A
	LD	A,20H
	INC	DE
	LD	(DE),A
	INC	DE
	LD	(DE),A
	INC	DE	;BUILD FILE TYPE 'SMC'
	LD	HL,COMBLK
	LD	BC,3
	LDIR
	XOR	A
	LD	(DE),A
	LD	(IY+32),A
;
;	INITIALIZE CP/M AND OPEN FILE
;
	LD	C,INITCM
	CALL	BDOS
	PUSH	IY	;OPEN FILE
	POP	DE
	LD	C,OPENCM
	CALL	BDOS
	INC	A
	JP	Z,NOFILE  ;IF NO FILE IS FOUND
	LD	HL,DMSG1 ;SEND MESSAGE 'FOUND FILE'
	CALL	DBUFFO
;
;	LOAD FILE INTO MEMORY STARTING AT 0100H
;
	LD	HL,0100H
	PUSH	HL
;
LOOP	POP	DE	;GET	NEXT RECORD ADDRESS
	PUSH	DE	;SAVE IT
	LD	C,SDMACM;GIVE IT TO CP/M
	CALL	BDOS
	PUSH	IY
	POP	DE
	LD	C,READCM;GET NEXT RECORD
	CALL	BDOS
	OR	A
	JP	NZ,DDONE   ;IF COMPLETE OR READ ERROR
	POP	DE	;INC TO NEXT RECORD BLOCK
	LD	HL,128	  ;GET NO OF BYTES PER RECORD
	ADD	HL,DE
	PUSH	HL	;SAVE NEW RECORD ADDRESS
	JP	LOOP
;
;	LOADING IS COMPLETE OR ERROR ENCOUNTERED
;
DDONE	DEC	A
	JP	NZ,DERROR   ;IF ERROR
	LD	HL,GOBACK ;LOAD RESTART ADDRESS
	EX	(SP),HL ;PUSH IT ONTO THE STACK
	JP	0100H	;JUMP TO THE TRANSIENT ROUTINE
;
;	ERROR MESSAGE AND VECTOR BACK TO MONITOR
;
DERROR	LD	HL,DMSG2
VBACK	CALL	DBUFFO
	LD	HL,GOBACK
	JP	(HL)
;
;	ESCAPE	..  A KEY WAS PRESSED
;
DGTOUT	LD	HL,DMSG3
	JP	VBACK
;
;	NO FILE MESSAGE
;
NOFILE	LD	HL,DMSG4
	JP	VBACK
;
;	MESSAGE MACROS
;
DMSG1	DEFB	CR,LF,'FOUND FILE',EOM
;
DMSG2	DEFB	CR,LF,'DISK READ ERROR',EOM
DMSG3	DEFB	CR,LF,'ESCAPE',EOM
;
DMSG4	DEFB	CR,LF,'FILE NOT FOUND',EOM
;
;	FILE NAME AND EXTENT BLOCKS
;
CPYBLK	DEFB	'COPY'
VFYBLK	DEFB	'VRFY'
COMBLK	DEFB	'SMC'
;
;	BOOT UP CP/M SYSTEM AND MODIFY
;	(THIS BOOT IS FOR SD SALES VERSAFLOOPY)
;	SET AT REAR OF DISK SUBSYSTEM FOR EASY MODIFICATION
;
BOOT	LD	SP,80H	       ;SET BOOT STACK
	LD	HL,80H
	LD	(40H),HL	;DMA ADDRESS (0000H)
	XOR	A
	LD	(42H),A		;LOAD DRIVE NO
	LD	(44H),A		;LOAD TRACK NO
	INC	A
	LD	(43H),A		;LOAD SECTOR NO
	LD	(45H),A		;NO OF SECTORS TO LOAD
	CALL	LOADER		;CALL SD VF LOADER
	JP	NZ,BTERR	;BAD LOAD
	LD	HL,BRETN	;LD RETURN VECTOR
	LD	(0C1H),HL
	JP	80H
;
BRETN	LD	HL,(06H)	;RETURNED SUCCESSFULLY
	LD	L,00		;GET JUMP VECTOR LOCATION
	LD	BC,0D07H	;FOR CONIN MONITORING
	ADD	HL,BC
	LD	DE,DCNSTA
	CALL	BRETNL
	LD	DE,DTRAP	;SET UP TRAP
	CALL	BRETNL
	LD	DE,DCNOUT
	CALL	BRETNL
	LD	DE,DLIST
	CALL	BRETNL
	JP	DBACK
;
BRETNL	LD	(HL),E
	INC	HL
	LD	(HL),D
	INC	HL
	INC	HL
	RET
;
BTERR	LD	HL,DMSG5
	JP	VBACK
;
DMSG5	DEFB	CR,LF,'BOOT LOADING ERROR',EOM
;
 